package com.snowfeel.sfmall.sys.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.snowfeel.sfmall.core.constants.EntityConstants;
import com.snowfeel.sfmall.core.enums.ResponseCode;
import com.snowfeel.sfmall.core.exception.BizException;
import com.snowfeel.sfmall.core.vo.PageVo;
import com.snowfeel.sfmall.sys.convert.SysPermissionConvert;
import com.snowfeel.sfmall.sys.dto.SysPermissionParam;
import com.snowfeel.sfmall.sys.mapper.SysMenuMapper;
import com.snowfeel.sfmall.sys.model.SysMenu;
import com.snowfeel.sfmall.sys.query.SysPermissionQuery;
import com.snowfeel.sfmall.sys.vo.SysPermissionNode;
import com.snowfeel.sfmall.sys.vo.SysPermissionVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 后台用户权限管理Service实现类
 * Created on 2018/9/29.
 */
@Service
public class SysPermissionService {

    @Autowired
    private SysMenuMapper menuMapper;

    @Autowired
    private SysPermissionConvert sysPermissionConvert;

    public int create(SysPermissionParam permission) {
        SysMenu sysMenu = sysPermissionConvert.convertPo(permission);
        sysMenu.setStatus(EntityConstants.YES);
        return menuMapper.insert(sysMenu);
    }

    public int update(Long id, SysPermissionParam permission) {
        SysMenu entity = menuMapper.selectById(id);
        if (Objects.isNull(entity)) {
            throw new BizException(ResponseCode.NOT_FOUND);
        }
        permission.setId(id);
        SysMenu sysMenu = sysPermissionConvert.convertPo(permission);
        return menuMapper.updateById(sysMenu);
    }

    public int delete(List<Long> ids) {
        return menuMapper.deleteBatchIds(ids);
    }

    public List<SysPermissionNode> treeList() {
        List<SysMenu> permissionList = menuMapper.selectList(null);
        return permissionList.stream()
                .filter(permission -> permission.getParentId().equals(0L))
                .map(permission -> covert(permission, permissionList)).collect(Collectors.toList());
    }

    /**
     * 将权限转换为带有子级的权限对象
     * 当找不到子级权限的时候map操作不会再递归调用covert
     */
    private SysPermissionNode covert(SysMenu permission, List<SysMenu> permissionList) {
        SysPermissionNode node = new SysPermissionNode();

        BeanUtils.copyProperties(node, permission);
        List<SysPermissionVo> children = permissionList.stream()
                .filter(subPermission -> subPermission.getParentId().equals(permission.getId()))
                .map(subPermission -> covert(subPermission, permissionList)).collect(Collectors.toList());
        node.setChildren(children);
        return node;
    }

    public PageVo<SysPermissionVo> page(SysPermissionQuery query) {
        LambdaQueryWrapper<SysMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(query.getName()), SysMenu::getMenuName, query.getName());
        queryWrapper.eq(Objects.nonNull(query.getStatus()), SysMenu::getStatus, query.getStatus());
        queryWrapper.orderByAsc(SysMenu::getSort);
        queryWrapper.orderByDesc(SysMenu::getCreateTime);
        Page<SysMenu> page = menuMapper.selectPage(new Page<>(query.getPageNo(), query.getPageSize()), queryWrapper);
        List<SysPermissionVo> permissionVoList = sysPermissionConvert.convertVoList(page.getRecords());
        PageVo<SysPermissionVo> pageVO = new PageVo<>();
        pageVO.setPages((int) page.getPages());
        pageVO.setTotal(page.getTotal());
        pageVO.setList(permissionVoList);
        return pageVO;
    }
}
